package package005

import com.google.gson.GsonBuilder
import entity.Classroom
import entity.User
import fundation.gsonPrintln
import fundation.println2
import org.junit.Test
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author futao
 * Created on 2017/11/10 - 16:50.
 */
class TestCase001 {
    @Test
    fun testCase001() {
        val list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        list.maxBy({ g -> g.name })
        list.maxBy { user: User -> user.id }
        list.maxBy(User::getId)
        println("=====" + GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(list.filter { it.sex > 1 }.map { it.name }))
        println("=====" + GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(list))
        println((list.maxBy { u: User -> u.name })!!.id)
        list.forEach({ println(it.name) })
        list.forEach { println(it.name) }
        println((list.find { it.sex == 8 })!!.name)
        println(list.findLast { it.sex == 8 }!!.name)
    }
//    @Test
//    fun testCase002(message:Collection<String>,str:String){
//        message.forEach{
//            println("$str ${User::getId}")
//            println("")
//        }
//    }

    @Test
    fun testCase003() {
        val map = mapOf(0 to "One", 1 to "Two", 3 to 3, "4" to "four")
        println(map[0])
        println(map.get(1))
        println(map.getOrDefault(3, ""))
        println(map.mapValues { it.value.toString().toUpperCase() })
        println(map.size)
        map[1].toString().first()
        map.map { it.value.toString().toUpperCase() }
        println(map["4"])
    }

    @Test
    fun testcase004() {
        val list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        println(list.all { it.sex > 0 })
        println(list.any { it.sex > 5 })
        println(list.count { it.sex > 5 })
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson((list.groupBy { it.sex > 5 }).filter { it.key }))
    }

    @Test
    fun testCase005() {
        val list = listOf(Classroom().apply {
            className = "weichaungBan"
            list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        }, Classroom().apply {
            className = "weichaungBan"
            list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        })
        println("========" + (list.flatMap { it.list }).toSet().toList())
        println("--------" + list.map { it.list })
    }

    @Test
    fun testCase() {
        val strings = listOf("abcf", "def")
        println(strings.flatMap { it.toList() })
        println(strings.flatMap { it.toList() }.toSet())
        println()
    }

    @Test
    fun testCase007() {
        val list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        val map = list.asSequence().filter {
            it.sex > 10
        }.map { user ->
            user.sex
        }.toList()
    }

    @Test
    fun testCase008() {
        val list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        val iterator = list.iterator()
        while (iterator.hasNext()) {
            println(GsonBuilder().setPrettyPrinting().create().toJson(iterator.next()))
            println("===================" + GsonBuilder().setPrettyPrinting().create().toJson(iterator))
        }
    }

    @Test
    fun testCase010() {
        val list = listOf(User("id001", "name001", 1), User("id008", "name008111", 8), User("id005", "name005", 5), User("id008", "name008222", 8))
        val sequence = list
                .asSequence()
                .filter {
                    println(it)
                    it.sex > 5
                }.map {
            it.name
            println(it.name)
        }
        println("======================")
        sequence.toList()
    }

    @Test
    fun testCase011() {
        val generateSequence = generateSequence(0, { it + 1 })
        val takeWhile = generateSequence.takeWhile { it <= 100 }
        var sum: Int = 0
        for (i in takeWhile) {
            sum += i
        }
        println(sum)
    }

    @Test
    fun testCase012() {
        val list = listOf(listOf(listOf(1, 2, 3, 1), listOf(5, 68, 5)))
        val flatMap = list.flatMap { it.flatMap { it } }
        kotlin.run {
            println("11")
        }
        println(flatMap)
    }

    @Test
    fun testCase013() {
        var result = StringBuilder()
        for (i in 'A'..'Z') {
            result.append(i)
        }
        result.append("HELLO")
        result.toString()
        println(result)
        var resultNew = StringBuilder()
        println(with(resultNew) {
            for (i in 'a'..'z') {
                this.append(i)
            }
            append("hhhhhh")
            toString()
        })
        var resultNew1 = StringBuilder()
        val apply = resultNew1.apply {
            for (i in 'A'..'Z') {
                append(i)
            }
            append("HELLO")
//            toString()
        }
        println("AAAAAAAAAAAAAAPPPPPPPP" + apply)
    }

    @Test
    fun testCase014() {
        var list = ArrayList<User>()
        repeat(10) {
            val let = User().apply {
                id = "id" + Random().nextInt().toString()
                sex = Random().nextInt()
                name = "name" + it
            }.also { list.add(it) }
        }
        list.println2()
        println(list)


        fun a(str: String?) {
//            val s = str ?: ""
            println(str?.length ?: throw NullPointerException("空指针异常"))
        }

        fun b(str: String?) {
            println(str!!.length)
        }
        try {
            val str: String? = null
            a(str)
        } catch (e: NullPointerException) {
            with(e) {
                println(message)
            }
        }
//        b(str)
    }

    @Test
    fun testCase015() {
        fun sendEmail(email: String) {

        }

        val email: String? = null
        fun CharSequence?.isNullOrBlank2(): Boolean = this == null || this.isEmpty()
        email.isNullOrBlank2()
        email.isNullOrEmpty()
        val let = email?.let { sendEmail(it) }
    }

    @Test
    fun testCase016() {
        val list = listOf(1L, 2L, 3L)
        (10.toLong() in list)

    }

    @Test
    fun testCase017() {
        fun case(): Nothing {
            println("")
            throw NullPointerException("")
        }
    }

    @Test
    fun testCase018() {
        val str = "111"
        val toIntOrNull = str.toIntOrNull(10)
        println(toIntOrNull)
        val arrayOf = arrayListOf("")
        val arrayOf1 = arrayOf("")
        val mutableListOf = mutableListOf("")
        mutableListOf.add("")
        arrayOf.add("")
        val intArray = IntArray(5) { i -> i * i }
        intArray.forEach {
            println("========" + it)
        }
        val intArray1 = IntArray(10)
        intArray1.all { it > 10 }
        println("+++++++++++++++++++" + intArray[0])
        val array = Array(26, { it -> "a" + it })
        array.forEach { println(it) }
    }

    @Test
    fun testCase019() {
        val strings = listOf("a", "b", "c")
        println("%s/%s".format(*strings.toTypedArray()))
        doubleArrayOf(11.0, 22.0)
        val intArray = IntArray(10)
        val charArray = CharArray(10) { it -> 'a' + it }
        intArray.forEach { println(it) }
        charArray.forEach {
            println(it)
        }
        charArray.forEachIndexed { i, c ->
            run {
                println("iiiiiii" + i)
                println("xxxxxxxxxxx" + c)
            }
        }
    }

    @Test
    fun testCase020() {
        val a = 10
        val b = 10
        val c = "a"
        val d = "a"
        gsonPrintln(a == b)
        gsonPrintln(a === b)

        gsonPrintln(c == c)
        gsonPrintln(d === d)
    }

    /**
     * kotlin中三个双引号    内部内容保留格式输出
     */
    @Test
    fun testCase021() {
        println("""  ""阿三大“”""////\\\\蘇打  """)
    }

}